﻿using System.Collections.Generic;
using Lifebucket.CommandProcessor.Command;
using Lifebucket.Domain.Commands;
using Lifebucket.Core.Common;
using Lifebucket.Data.Repositories;
using Lifebucket.Data.Infrastructure;
using Lifebucket.Model;
using Lifebucket.Domain.Properties;
namespace Lifebucket.Domain.Handlers
{
    public class CanAddBucketItem : IValidationHandler<CreateOrUpdateBucketItemCommand>
    {
        private readonly IBucketItemRepository bucketItemRepository;
        public CanAddBucketItem(IBucketItemRepository bucketItemRepository, IUnitOfWork unitOfWork)
        {
            this.bucketItemRepository = bucketItemRepository;           
        }
        public IEnumerable<ValidationResult> Validate(CreateOrUpdateBucketItemCommand command)
        {
            BucketItem isBucketItemExists=null;
            if(command.BucketItemId==0)
                isBucketItemExists = bucketItemRepository.Get(b => b.Label == command.Label);
            else
                isBucketItemExists = bucketItemRepository.Get(b => b.Label == command.Label && b.BucketItemId != command.BucketItemId);
            if (isBucketItemExists != null)
            {
                yield return new ValidationResult("Label", Resources.BucketItemExists);
            }
        }
    }
}
